package Server.junit;

import common.exception.ExtinctionException;

import Server.players.Carnivorous;
import Server.players.Dino;
import Server.players.Player;
import Server.players.PlayerList;
import junit.framework.TestCase;

public class PlayerListTest extends TestCase {

	private PlayerList p;
	private Player p1;
	private Player p2;
	protected void setUp() throws Exception {
		super.setUp();
		this.p = new PlayerList();
		this.p1 = new Player("pippo", "pluto");
		this.p2 = new Player("foo", "bar");
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testIsInGame() {
		assertFalse(p.isInGame(this.p1));
		this.p.add(this.p1);
		this.p.inGame(this.p1);
		assertFalse(p.isInGame(this.p1));
		this.p.login(this.p1.getName(), "pluto");
		if (this.p.inGame(this.p1))
			assertTrue(this.p.isInGame(this.p1));
	}

	public void testGet() {
		assertNull(this.p.get(0));
		this.p.add(this.p1);
		int token = this.p.login(this.p1.getName(), "pluto");
		assertEquals(this.p1, this.p.get(token));
	}

	public void testGetInGamePlayers() {
		assertTrue(this.p.getInGamePlayers().length == 0);
		this.p.add(this.p1);
		this.p.login(this.p1.getName(), "pluto");
		if (this.p.inGame(this.p1)){
			assertTrue(this.p.getInGamePlayers().length == 1);
			assertEquals(this.p.getInGamePlayers()[0], this.p1.getName());
		}
	}

	public void testGetCurrentPlayer() {
		assertNull(this.p.getCurrentPlayer());
		this.p.add(this.p1);
		this.p.add(this.p2);
		this.p.login(this.p1.getName(), "pluto");
		this.p.login(this.p2.getName(), "bar");
		if (this.p.inGame(this.p1) && this.p.inGame(this.p2)){
			assertEquals(this.p.getCurrentPlayer(), this.p1);
		}
	}

	public void testExistsSpecies() {
		assertFalse(this.p.existsSpecies("specie"));
		this.p1.newSpecies("specie", 'c');
		this.p.add(this.p1);
		assertTrue(this.p.existsSpecies("specie"));
	}

	public void testExists() {
		assertFalse(this.p.exists(this.p1.getName()));
		this.p.add(this.p1);
		assertTrue(this.p.exists(this.p1.getName()));
	}

	public void testExistsDino() {
		assertFalse(this.p.existsDino(new Carnivorous(1)));
		this.p1.newSpecies("specie", 'c');
		this.p1.newDino(1);
		this.p.add(this.p1);
		assertTrue(this.p.existsDino(new Carnivorous(1)));
	}

	public void testNextCurrentPlayer() {
		assertNull(this.p.nextCurrentPlayer());
		this.p.add(this.p1);
		this.p.add(this.p2);
		this.p.login(this.p1.getName(), "pluto");
		this.p.login(this.p2.getName(), "bar");
		if (this.p.inGame(this.p1) && this.p.inGame(this.p2)){
			assertEquals(this.p.getCurrentPlayer(), this.p1);
			assertEquals(this.p.nextCurrentPlayer(), this.p2);
			assertEquals(this.p.nextCurrentPlayer(), this.p1);
		}
	}

	public void testLogin() {
		assertTrue(this.p.login(this.p1.getName(), "pluto") == -1);
		this.p.add(this.p1);
		assertTrue(this.p.login(this.p1.getName(), "pluto") > -1);
		assertNotNull(this.p.get(this.p.login(this.p1.getName(), "pluto")));
	}

	public void testLogout() {
		this.p.add(this.p1);
		if (this.p.login(this.p1.getName(), "pluto") > -1){
			int token = this.p.login(this.p1.getName(), "pluto");
			this.p.logout(this.p1);
			assertNull(this.p.get(token));
		}
	}

	public void testInGame() {
		this.p.add(this.p1);
		this.p.inGame(this.p1);
		assertFalse(p.isInGame(this.p1));
		this.p.login(this.p1.getName(), "pluto");
		if (this.p.inGame(this.p1))
			assertTrue(this.p.isInGame(this.p1));
	}

	public void testOutGame() {
		this.p.add(this.p1);
		this.p.inGame(this.p1);
		assertFalse(p.isInGame(this.p1));
		this.p.login(this.p1.getName(), "pluto");
		if (this.p.inGame(this.p1)){
			assertTrue(this.p.isInGame(this.p1));
			this.p.outGame(this.p1);
			assertFalse(this.p.isInGame(this.p1));
		}
	}

	public void testGetOwner() {
		Dino c = new Carnivorous(1);
		this.p1.newSpecies("specie", 'c');
		this.p1.newDino(1);
		this.p.add(this.p1);
		assertEquals(this.p.getOwner(c), this.p1);
	}

	public void testGetDino() {
		this.p1.newSpecies("specie", 'c');
		this.p1.newDino(1);
		this.p.add(this.p1);
		assertEquals(new Carnivorous(1), this.p.getDino(1));
	}

	public void testUpdateAges() {
		this.p1.newSpecies("specie", 'c');
		this.p.add(this.p1);
		int token = this.p.login(this.p1.getName(), "pluto");
		this.p.inGame(this.p1);
		assertTrue(this.p1.getSpeciesAge() == 1);
		try{
			this.p.updateAges();
		} catch (ExtinctionException e){
			fail();
		}
		this.p1 = this.p.get(token);
		assertTrue(this.p1.getSpeciesAge() == 2);
	}

}
